/**
 * ModelsDao.java
 *
 * Copyright (c) 2009 prjmanager All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of prjmanager .
 **/

package ro.prjmanager.dao;

import org.hibernate.Query;
import ro.prjmanager.core.dao.GenericDAO;
import ro.prjmanager.model.Models;
import ro.prjmanager.model.Projects;

import java.util.List;

/**
 * This interface provides custom method declaration for Models
 * entity and has a default implementation in the same package :
 * ModelsDAOImpl
 * @author Viorel Tacliciu
 * @version 0.0.1
 */
@SuppressWarnings("unchecked")
public class ModelsDao extends GenericDAO<Models,  java.lang.Integer> {
    @Override
    public List<Models> findAll() {
        Integer idProject=((Projects)getUserSession().getProject()).getId();
        Query query=getSession().createQuery("select c from "+getPersistentClass().getName()+" as c where c.fkProjectDb.fkProject.id="+idProject);
        return query.list();
    }

    /**
     * This method is trying  to search an models with the specified dataBaseName field
     * Also the search operation will take care about user login project
     * @param databaseName - String - represents the table name as is it in DATABASE
     * @return - the models object corresponding to the dataBaseName field
     */
    public Models findByDataBaseName(String databaseName){
        Integer idProject=((Projects)getUserSession().getProject()).getId();
        Query query=getSession().
                createQuery("select c from "+getPersistentClass().getName()+" as c where c.databaseName='" +databaseName+
                        "' and c.fkProjectDb.fkProject.id="+idProject);
        List<Models> temp=query.list();
        if(temp!=null&&!temp.isEmpty())
            return temp.get(0);
        return null;
    }

    public Models findTableSet(String tableName){
        Integer idProject=((Projects)getUserSession().getProject()).getId();
          Query query=getSession().
                createQuery("select c from "+getPersistentClass().getName()+" as c where c.tableName='" +tableName+
                        "' and c.fkProjectDb.fkProject.id="+idProject);
        List<Models> temp=query.list();
        if(temp!=null&&!temp.isEmpty())
            return temp.get(0);
        return null;
    }
}
